package com.feri.cloudlike.provider.service.impl;

import com.feri.cloudlike.common.dto.LikeAddDto;
import com.feri.cloudlike.common.vo.R;
import com.feri.cloudlike.entity.Like;
import com.feri.cloudlike.provider.dto.LikeDao;
import com.feri.cloudlike.provider.service.intf.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * projectName: Cloudlike
 * author: CGY
 * time: 2020/10/30 17:53
 * description:
 */
@Service
public class LikeServiceImpl implements LikeService {
    @Autowired
    private LikeDao dao;
    @Override
    public R likev1(LikeAddDto dto) {
        //发起点赞
        if (dto != null && dto.getCid()>0 && dto.getUid()>0){
            //1.校验是否点赞
            Like like = dao.querySingle(dto);
            if (like == null){
                //需要点赞
                //新增
                if (dao.insert(dto) > 0){
                    return R.ok();
                }else {
                    return R.fail("亲，点赞失败");
                }
            } else {
                //取消点赞
                //删除
                if (dao.del(dto) > 0) {
                    return R.ok();
                } else {
                    return R.fail("亲，取消点赞失败，稍后再来");
                }
            }
        }else{
            return R.fail("亲，参数非法");
        }
    }

    @Override
    public R queryCount() {
        return R.ok(dao.queryCount());
    }
}
